'''
批量修改文件下的文件名
'''

import os


def remove_chars_keep_extension(folder_path):
    """
    移除文件名中的特定字符（保持扩展名不变）
    :param folder_path: 文件夹路径
    """
    for filename in os.listdir(folder_path):
        # 分离文件名和扩展名
        name_part, ext = os.path.splitext(filename)

        # 移除多余的
        end = name_part.find("【")
        if end == -1:
            continue
        name_part = name_part[:end]
        print(name_part)

        # 重命名
        new_filename = f"{name_part}{ext}"
        old_path = os.path.join(folder_path, filename)
        new_path = os.path.join(folder_path, new_filename)
        os.rename(old_path, new_path)
        print(f"Renamed: {filename} -> {new_filename}")


# 使用示例：移除文件名中的空格和括号
remove_chars_keep_extension("/Users/zwx/Desktop/advance/AI/java-ai/1.课件/images")